package org.feezu.liuli.timeselector.Utils;

import android.text.TextUtils;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by liuli on 2015/11/27.
 */
public class DateUtil {

    public static final String YYYY = "yyyy";

    public static final String YYYYMM = "yyyy-MM";

    public static final String YYYYMMDD = "yyyy-MM-dd";

    public static final String YYYYMMDDHH = "yyyy-MM-dd HH";

    public static final String YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";

    public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 使用用户格式提取字符串日期
     *
     * @param strDate 日期字符串
     * @param pattern 日期格式
     * @return
     */

    public static Date parse(String strDate, String pattern) {

        if (TextUtils.isEmpty(strDate)) {
            return null;
        }
        try {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 使用用户格式格式化日期
     *
     * @param date    日期
     * @param pattern 日期格式
     * @return
     */

    public static String format(Date date, String pattern) {
        String returnValue = "";
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            returnValue = df.format(date);
        }
        return (returnValue);
    }

    /**
     * 将时间戳转化为   日期格式
     *
     * @param time    时间戳
     * @param pattern 格式化日期格式
     * @return
     */
    public static String format(long time, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(new Date(time));
    }

    /**
     * 功能描述：返回小时
     *
     * @param
     * @return 返回小时
     */
    public static String getHourMill(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//只有时分秒
        String s = sdf.format(new Date(time * 1000));
        Log.d("getHour", s + "");
        return s;
    }

    /**
     * 功能描述：返回小时
     *
     * @param
     * @return 返回小时
     */
    public static String getHourMill3(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//只有时分秒
        String s = sdf.format(new Date(time * 1000));
        Log.d("getHour", s + "");
        return s;
    }

    /**
     * 功能描述：返回小时
     *
     * @param
     * @return 返回小时
     */
    public static String getHourMill2(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");//只有时分秒
        String s = sdf.format(new Date(time * 1000));
        Log.d("getHour", s + "");
        return s;
    }

    public static final int YEAR_RETURN = 0;

    public static final int MONTH_RETURN = 1;

    public static final int DAY_RETURN = 2;

    public static final int HOUR_RETURN = 3;

    public static final int MINUTE_RETURN = 4;

    public static final int SECOND_RETURN = 5;


    public static long getBetween(String beginTime, String endTime, String formatPattern, int returnPattern) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatPattern);
        Date beginDate = simpleDateFormat.parse(beginTime);
        Date endDate = simpleDateFormat.parse(endTime);

        Calendar beginCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        beginCalendar.setTime(beginDate);
        endCalendar.setTime(endDate);
        switch (returnPattern) {
            case YEAR_RETURN:
                return getByField(beginCalendar, endCalendar, Calendar.YEAR);
            case MONTH_RETURN:
                return getByField(beginCalendar, endCalendar, Calendar.YEAR) * 12 + getByField(beginCalendar, endCalendar, Calendar.MONTH);
            case DAY_RETURN:
                return getTime(beginDate, endDate) / (24 * 60 * 60 * 1000);
            case HOUR_RETURN:
                return getTime(beginDate, endDate) / (60 * 60 * 1000);
            case MINUTE_RETURN:
                return getTime(beginDate, endDate) / (60 * 1000);
            case SECOND_RETURN:
                return getTime(beginDate, endDate) / 1000;
            default:
                return 0;
        }
    }

    private static long getByField(Calendar beginCalendar, Calendar endCalendar, int calendarField) {
        return endCalendar.get(calendarField) - beginCalendar.get(calendarField);
    }

    private static long getTime(Date beginDate, Date endDate) {
        return endDate.getTime() - beginDate.getTime();
    }


}
